package com.jiuzhi.server.handler;

import java.net.InetSocketAddress;
import java.util.HashMap;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jiuzhi.util.Initialization;

public class MinaLongConnServerHandler extends IoHandlerAdapter {

    private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());

 

    @Override

    public void sessionOpened(IoSession session) {

       InetSocketAddress remoteAddress = (InetSocketAddress)session.getRemoteAddress();

       String clientIp = remoteAddress.getAddress().getHostAddress();

       logger.info("LongConnect Server opened Session ID ="+String.valueOf(session.getId()));

       logger.info("接收来自客户端 :" + clientIp + "的连接.");

       Initialization init = Initialization.getInstance();

       HashMap<String, IoSession> clientMap =init.getClientMap();

       clientMap.put(clientIp, session);
       
       

    }

 

    @Override

    public void messageReceived(IoSession session, Object message) {

       logger.info("Messagereceived in the long connect server..");

       String expression = message.toString();

       logger.info("Message is:" + expression);

//       IoSession shortConnSession =(IoSession) session.getAttribute("shortConnSession");
//
//       logger.info("ShortConnect Server Session ID ="+String.valueOf(shortConnSession.getId()));
//
//       shortConnSession.write(expression);
      session.write(" 相应客户端消息-----唐思想---");

    }

 

    @Override

    public void sessionIdle(IoSession session, IdleStatus status) {

       logger.info("Disconnectingthe idle.");

       // disconnect an idle client

       session.close(true);

    }

 

    @Override

    public void exceptionCaught(IoSession session, Throwable cause) {

       // close the connection onexceptional situation

       logger.warn("---关闭成功：----"+cause.getMessage(), cause);
       InetSocketAddress remoteAddress = (InetSocketAddress)session.getRemoteAddress();
       String clientIp = remoteAddress.getAddress().getHostAddress();
       logger.error("远程客户端IP地址关闭了：%s",clientIp);
       Initialization.getInstance().getClientMap().remove(clientIp);
       
       session.close(true);

    } 

}
